package com.yonyou.pmclouds.basecom.conf;

import com.yonyou.pmclouds.basecom.filter.MobileTokenCheckFilter;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class MobileFilterConfiguration {

    private static final String URL_PATTERN_MOBILE = "/mobile/*";
    //不参与token校验的移动端请求路径
    public static final String IGNORE_TOKEN_URLS_PARAMTER = "ignoreTokenUrls";
    private static final String IGNORE_TOKEN_URLS_VALUES = "/mobile/mobLogin/*,/mobile/mobReg/*,/mobile/resetpassword/*,/mobile/uspace/ssoLogin";

    @Bean
    @Profile("!pressure")
    public FilterRegistrationBean mobileTokenCheckFilter() {
        Filter filter = new MobileTokenCheckFilter();
        FilterRegistrationBean bean = new FilterRegistrationBean(filter);
        bean.setUrlPatterns(Arrays.asList(URL_PATTERN_MOBILE));
        bean.setOrder(FilterLevelConst.LEVEL_FIRST);
        Map<String, String> parmMap = new HashMap<String, String>();
        parmMap.put(IGNORE_TOKEN_URLS_PARAMTER, IGNORE_TOKEN_URLS_VALUES);
        bean.setInitParameters(parmMap);
        return bean;
    }

    @Bean
    @Profile("pressure")
    public FilterRegistrationBean pressureMockerFilter(final PressureTestProperties properties) {
        Filter filter = new Filter() {
            @Override
            public void init(FilterConfig filterConfig) throws ServletException {

            }

            @Override
            public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
                try {
                    Thread.sleep(properties.getDelay());
                    RuntimeEnvironment.setLoginUserId(properties.getUser());
                    if (request instanceof HttpServletRequest) {
                        String header = ((HttpServletRequest) request).getHeader("User-Id");
                        if (StringUtils.isNotEmpty(header)) {
                            RuntimeEnvironment.setLoginUserId(header);
                        }
                    }
                } catch (InterruptedException e) {
                } finally {
                    chain.doFilter(request, response);
                }

            }

            @Override
            public void destroy() {

            }
        };
        FilterRegistrationBean bean = new FilterRegistrationBean(filter);
        bean.setUrlPatterns(Collections.singletonList(URL_PATTERN_MOBILE));
        bean.setOrder(FilterLevelConst.LEVEL_FIRST);
        return bean;
    }

    @Setter
    @Getter
    @Profile("pressure")
    @ConfigurationProperties(prefix = "pressure")
    public static class PressureTestProperties {
        private String user;
        private long delay;
    }

}

@Configuration
@EnableConfigurationProperties(MobileFilterConfiguration.PressureTestProperties.class)
class PressureTestPropertiesConfig {

}


